package com.smileus.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.ibatis.common.util.Page;
import com.smileus.common.Env;
import com.smileus.entity.IntegrationLogs;

public class IntegrationLogsService extends BaseService {
	private static BlockingQueue<IntegrationLogs> logsQueue = new LinkedBlockingQueue<IntegrationLogs>();
	static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

	// 3秒钟后运行，并每隔10秒运行一次
	public static void startLogJob() {
		final Runnable logHandler = new Runnable() {
			public void run() {
				List<IntegrationLogs> list = new ArrayList<IntegrationLogs>();
				logsQueue.drainTo(list);
				if (list.size() > 0) {
					Env.getBean("baseService", BaseService.class).getHibernateDAO().btachSave(list);
				}
			}
		};
		// 1秒钟后运行，并每隔10秒运行一次
		scheduler.scheduleAtFixedRate(logHandler, 1, 10, TimeUnit.SECONDS);
		scheduler.submit(logHandler);
	}

	public static void saveLogs(IntegrationLogs logs) {
		logsQueue.add(logs);
	}

	static String IG_HQL = " from IntegrationLogs ";

	public Page<IntegrationLogs> findPage(Page<IntegrationLogs> page) {
		return findPageByArray(page, IG_HQL);
	}
}
